#include "head_s.h"

int delete_directory(const char *directory_path) {
    //删除目录的具体执行
    DIR *d = opendir(directory_path);
    size_t path_len = strlen(directory_path);
    int result = 0;

    if (!d) {
        return 1; // 返回1表示失败
    }

    //忽略.和..
    struct dirent *p;
    while ((p = readdir(d)) != NULL) {
        if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) {
            continue;
        }

        char *sub_path = (char *)malloc(path_len + strlen(p->d_name) + 2);
        if (!sub_path) {
            closedir(d);
            return 1; // 返回1表示失败
        }

        sprintf(sub_path, "%s/%s", directory_path, p->d_name);

        //查询文件,并删除
        struct stat info;
        if (stat(sub_path, &info) != 0) {
            free(sub_path);
            closedir(d);
            return 1; // 返回1表示失败
        }

        if (S_ISDIR(info.st_mode)) {
            result = delete_directory(sub_path);//再次删除目录
            if (result != 0) {
                free(sub_path);
                closedir(d);
                return 1; // 返回1表示失败
            }
        } else {
            if (remove(sub_path) != 0) {
                free(sub_path);
                closedir(d);
                return 1; // 返回1表示失败
            }
        }

        free(sub_path);
    }

    closedir(d);

    if (rmdir(directory_path) != 0) {
        return 1; // 返回1表示失败
    }

    return 0; // 返回0表示成功
}

int rm_dir(const user_t* status, const portocol_t* portocol, int net_fd)
{
    char path_last[500]={0};
    strcat(path_last,status->base_path);
    strcat(path_last,status->user_name);
    int len=strlen(path_last);
    //path_last[len]='/';
    strcat(path_last,status->path);
    strcat(path_last,status->arg);
    int len_1=strlen(path_last);
    //path_last[len_1]='/';
    path_last[len_1+1]='\0';
    printf("path:%s",path_last);
    int result = delete_directory(path_last);
    //接收删除指令并执行删除目录

    char buf1[10]={0};
    if (result == 0) {
        buf1[0]='0';
        //printf("删除操作成功\n");
    } else {
        buf1[0]='1';
        //printf("删除操作失败\n");
    }
    send(net_fd,buf1,sizeof(buf1), MSG_NOSIGNAL);
}







int rm_file(const user_t* status, const portocol_t* portocol, int net_fd)
{
    int ret;
    //删除文件
    printf("文件\n");

     char path_last[500]={0};
    strcat(path_last,status->base_path);
    strcat(path_last,status->user_name);
    int len=strlen(path_last);
    strcat(path_last,status->path);
    strcat(path_last,status->arg);
    int len_1=strlen(path_last);
    path_last[len_1+1]='\0';


    ret = remove(path_last);
    char buf1[10]={0};

    struct dirent* pdirent;
    if(ret == 0) 
    {
        buf1[0]='0';
        
        send(net_fd,buf1,sizeof(buf1), MSG_NOSIGNAL);          
        
        //printf("文件删除成功");
    }
    else 
    {
        buf1[0]='1';
        send(net_fd,buf1,sizeof(buf1), MSG_NOSIGNAL);
            // printf("错误：不能删除该文件");
    }
}

int rm(const user_t* status, const portocol_t* portocol, int net_fd)
{   
    char path_last[500]={0};
    strcat(path_last,status->base_path);
    strcat(path_last,status->user_name);
    int len=strlen(path_last);
    path_last[len+1]='\0';
    strcat(path_last,status->path);
    //拼接函数
    DIR* dir=opendir(path_last);
    if(dir==NULL){
    }

        struct dirent *curr;
    while((curr=readdir(dir)) != NULL){
        if(strcmp(curr->d_name, status->arg)==0){
            if (curr->d_type==DT_DIR){
                //删除目录
                rm_dir(status, portocol, net_fd);
                return 0;
            }else
            {
                //删除文件
                rm_file(status,portocol,net_fd);
                return 0;
            } 
            printf("删除结束\n");
        }
        printf("while结束\n");        
        
    }
    
    printf("打印失败\n");
    //失败返回1
    char buf2[10]={0};
    buf2[0]='1';
    send(net_fd,buf2,sizeof(buf2), MSG_NOSIGNAL);
    
    closedir(dir);
        return 0;
}
